import pygame
from pygame import Surface, image, event
from pygame.locals import *
from .network import *


class Role:
    def __init__(self) -> None:
        self.surface: Surface = image.load(
            "./dist/image/hero.png").convert_alpha()
        self.id = id
        # 人物相对地图坐标
        self.x: int = 100
        self.y: int = 100

        self.speed = [0, 0]
        self.status = [1, 0]

    def draw_frame(self, dest: Surface, source: Surface, x: int, y: int, cell_x: int, cell_y: int, cell_w: int = 32, cell_h: int = 32):
        dest.blit(source, (x, y), (cell_x * cell_w,
                                   cell_y * cell_h, cell_w, cell_h))

    def draw(self, screen: Surface, x: int, y: int):
        if self.speed[0] == 0 and self.speed[1] == 0:
            self.status[0] = 1
        else:
            self.status[0] += 0.1

        if self.speed[1] == -1:
            self.status[1] = 3
        elif self.speed[1] == 1:
            self.status[1] = 0
        elif self.speed[0] == -1:
            self.status[1] = 1
        elif self.speed[0] == 1:
            self.status[1] = 2

        self.draw_frame(screen, self.surface, x, y,
                        int(self.status[0] % 3), self.status[1])


class Player:
    def __init__(self, id: int = 0, x: int = 50, y: int = 50):
        self.surface: Surface = image.load(
            "./dist/image/hero.png").convert_alpha()
        self.id = id
        # 人物相对地图坐标
        self.x: int = 100
        self.y: int = 100

        self.speed = [0, 0]
        self.status = [1, 0]

    def eventHandler(self, even: event.Event):
        if even.type == KEYDOWN:
            if even.key == pygame.K_UP:
                print("up")
                self.status[1] = 3
                self.speed[1] = -1
                self.speed[0] = 0
            elif even.key == pygame.K_DOWN:
                print("down")
                self.status[1] = 0
                self.speed[1] = 1
                self.speed[0] = 0
            elif even.key == pygame.K_LEFT:
                print("left")
                self.status[1] = 1
                self.speed[0] = -1
                self.speed[1] = 0
            elif even.key == pygame.K_RIGHT:
                print("right")
                self.status[1] = 2
                self.speed[1] = 0
                self.speed[0] = 1
        elif even.type == KEYUP:
            if even.key == pygame.K_UP:
                self.speed[1] = 0
            elif even.key == pygame.K_DOWN:
                self.speed[1] = 0
            elif even.key == pygame.K_LEFT:
                self.speed[0] = 0
            elif even.key == pygame.K_RIGHT:
                self.speed[0] = 0

    def draw_frame(self, dest: Surface, source: Surface, x: int, y: int, cell_x: int, cell_y: int, cell_w: int = 32, cell_h: int = 32):
        dest.blit(source, (x, y), (cell_x * cell_w,
                                   cell_y * cell_h, cell_w, cell_h))

    def draw(self, screen: Surface, x: int, y: int):
        self.draw_frame(screen, self.surface, x, y,
                        int(self.status[0] % 3), self.status[1])

    def move(self, wakleable_map: list):
        if self.speed[1] == -1 and wakleable_map[int((self.y-1)/32)][int((self.x+16)/32)] == 0 and self.y >= 0:
            self.x += self.speed[0]
            self.y += self.speed[1]
        elif self.speed[1] == 1 and wakleable_map[int((self.y+33)/32)][int((self.x+16)/32)] == 0 and self.y <= 640:
            self.x += self.speed[0]
            self.y += self.speed[1]
        elif self.speed[0] == -1 and wakleable_map[int((self.y+16)/32)][int((self.x-1)/32)] == 0 and self.x >= 0:
            self.x += self.speed[0]
            self.y += self.speed[1]
        elif self.speed[0] == 1 and wakleable_map[int((self.y+16)/32)][int((self.x+33)/32)] == 0 and self.x <= 1216:
            self.x += self.speed[0]
            self.y += self.speed[1]

        Net.sendActions(self)
        if self.speed[0] == 0 and self.speed[1] == 0:
            self.status[0] = 1
        else:
            self.status[0] += 0.1
